﻿#include "CTaskQueue.h"
#include "CTcpTask.h"

CTaskList::CTaskList()
{
}

CTaskList::~CTaskList() {};

void setEpfd(int _epfd)
{
	this->_M_epfd = _epfd;
}

bool CTaskList::empty() const
{
	return _M_tasks.empty();
}

void _add(CTcpTask* pTask)
{
	this->_M_tasks.push_back(pTask);
}

void CTaskList::remove(CTcpTask* _pTask)
{
	_tasks_iter iter = this->_M_tasks.begin();
	for(; iter != this->_M_tasks.end(); ++iter)
	{
		if(*iter == _pTask)
		{
			this->_M_tasks.erase(iter);
			return;
		}
	}
}

void CTaskList::remove(_tasks_iter& iter)
{
	if(iter != this->_M_tasks.end())
		this->_M_tasks.erase(iter);
}


/// 从列表上拿出一个
CTcpTask* CTaskList::pop()
{
	if(_M_tasks.empty())
	{
		return NULL;
	}

	CTcpTask* pTask = NULL;
	pTask = this->_M_tasks.front();
	this->_M_tasks.pop_front();
	return pTask;
}

CTaskQueue::CTaskQueue()
{}

CTaskQueue::~CTaskQueue()
{}

/**
 * \brief 压入
 * \param _pTask：任务
 */
void CTaskQueue::push(CTcpTask* _pTask)
{
	this->_M_mutex.lock();
	this->_M_queue.push(_pTask);
	this->_M_mutex.unlock();
}

/**
 * \brief 弹出
 * \return 任务
 */
CTcpTask* CTaskQueue::pop()
{
	CTcpTask* pTask = NULL;

	this->_M_mutex.lock();
	if(!this->_M_queue.empty())
	{
		pTask = this->_M_queue.front();
		this->_M_queue.pop();
	}
	this->_M_mutex.unlock();

	return pTask;
}

/**
 * \brief 导出所有
 * \param _taskList: 导出容器
 */
void CTaskQueue::checkExport(CTaskList& _taskList)
{
	this->_M_mutex.lock();
	while(!this->_M_queue.empty())
	{
		CTcpTask* pTask = this->_M_queue.front();
		this->_M_queue.pop();
		_taskList._add(pTask);
	}
	this->_M_mutex.unlock();
}


